package com.heima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.pojos.ArticleConfig;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.service.ArticleConfigService;
import com.heima.mapper.ArticleConfigMapper;
import org.springframework.stereotype.Service;

/**
* @author chenshan
* @description 针对表【ap_article_config(APP已发布文章配置表)】的数据库操作Service实现
* @createDate 2022-10-14 17:01:13
*/
@Service
public class ArticleConfigServiceImpl extends ServiceImpl<ArticleConfigMapper, ArticleConfig>
    implements ArticleConfigService{

    /**
     * 文章的上架或下架
     * @param news 文章
     * @return ResponseResult
     */
    @Override
    public ResponseResult articleConfigDownOrUp(WmNews news) {
        // 参数的合法性校验
        if (!ObjectUtil.isAllNotEmpty(news.getArticleId(), news.getEnable())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 查询文章配置信息
        ArticleConfig articleConfig = getOne(Wrappers.lambdaQuery(ArticleConfig.class).eq(ArticleConfig::getArticleId, news.getArticleId()));

        if (ObjectUtil.isNull(articleConfig)) {
            return ResponseResult.errorResult(5000, "没有这篇文章~");
        }

        // 修改文章是否上架
        if (news.getEnable().equals((short) 0)) {
            articleConfig.setIsDown(1);
        } else {
            articleConfig.setIsDown(0);
        }

        updateById(articleConfig);
        return ResponseResult.okResult(null);
    }
}




